/**
 *  Copyright 2002-2009 the original author or authors.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package foo.bar.blog.core.services;

import foo.bar.blog.core.domain.Property;
import foo.bar.blog.core.domain.User;
import org.jsecurity.SecurityUtils;
import org.jsecurity.subject.Subject;
import org.springframework.web.servlet.ThemeResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Used by various action beans that extends off {@link foo.bar.blog.web.action.BaseActionBean}
 * to determine the current non-blog's theme name (eg. administratio console etc.).
 * <p/>
 * It resolves the theme name as follows with descending priority:-
 * <ul>
 *  <li>From session attribute named {@link #getThemeNameKey()} </li>
 *  <li>From {@link foo.bar.blog.core.domain.User}'s {@link foo.bar.blog.core.domain.Property} named {@link #getThemeNameKey()} </li>
 *  <li>Default theme name in {@link #getDefaultThemeName()} </li>
 * </ul>
 * 
 * @see {@link foo.bar.blog.web.extension.ThemeInterceptor}
 *
 * @author tmjee
 * @version $Date: 2009-03-14 06:53:07 +0000 (Sat, 14 Mar 2009) $ $Id$
 */
public class DefaultThemeResolver implements ThemeResolver {

    private String defaultThemeName = "default";
    private String themeNameKey = "themeName" ;

    public void setDefaultThemeName(String defaultThemeName) {
        this.defaultThemeName = defaultThemeName;
    }

    public String getDefaultThemeName() {
        return defaultThemeName;
    }

    public void setThemeNameKey(String themeNameKey) {
        this.themeNameKey = themeNameKey;
    }

    public String getThemeNameKey() {
        return themeNameKey;
    }

    public String resolveThemeName(HttpServletRequest request) {
        String reqThemeName = request.getParameter(themeNameKey);
        if (reqThemeName != null && reqThemeName.trim().length() > 0) {
            // use theme in request parameter
            return reqThemeName.trim();
        }
        String sesThemeName = (String) request.getSession(true).getAttribute(themeNameKey);
        if (sesThemeName != null && sesThemeName.trim().length() > 0) {
            // use theme in session attribute
            return sesThemeName;
        }
        Object principal = getCurrentPrincipal();
        if ((principal != null) && (principal instanceof User)) {
            User user = (User) principal;
            Property property = user.getProperty(themeNameKey);
            if (property != null) {
                return property.getValue()==null ? defaultThemeName : property.getValue().trim();
            }
        }
        return defaultThemeName;
    }

    public void setThemeName(HttpServletRequest request, HttpServletResponse response, String themeName) {
        request.getSession(true).setAttribute(themeNameKey, themeName);
    }

    protected Object getCurrentPrincipal() {
        Subject subject = SecurityUtils.getSubject();
        if (subject != null) {
            return subject.getPrincipal();
        }
        return subject;
    }
}
